package com.mall507.fivepeaks.activity.main;

import android.os.Bundle;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.hzh.frame.ui.fragment.AbsRecyclerViewFM;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.mall507.comn.model.MineMessage;
import com.mall507.comn.model.Original;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.widget.imageview.CircleImageView;
import com.mall507.fivepeaks.widget.imageview.ExpandImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 五峰原创
 */
public class MainTownShipDongTaiRFM extends AbsRecyclerViewFM<MineMessage> {
    
    List<String> listUrl;

    public static MainTownShipDongTaiRFM newInstance(int type,String nid) {
        MainTownShipDongTaiRFM myFragment = new MainTownShipDongTaiRFM();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        bundle.putString("nid", nid);
        myFragment.setArguments(bundle);
        return myFragment;
    }

    @Override
    public boolean setTitleIsShow() {
        return false;
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fm_rv_main_township_dongtai;
    }

    @Override
    protected String setHttpPath() {
        return "Township/getTtownshipDynamicList";
    }

    @Override
    protected JSONObject setHttpParams() {
        JSONObject params = new JSONObject();
        try {
            params.put("id", getArguments().getString("nid"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return params;
    }

    @Override
    protected List<MineMessage> handleHttpData(JSONObject response) {
        List<MineMessage> list = new ArrayList<>();
        if (200 == response.optInt("code")) {
            JSONArray originals = response.optJSONArray("data");
            if (originals != null && originals.length() > 0) {
                for (int i = 0; i < originals.length(); i++) {
                    JSONObject object = originals.optJSONObject(i);
                    MineMessage model = new MineMessage();
                    model.setNid(object.optString("id"));
                    model.setTime(object.optString("create_time"));
                    model.setContent(object.optString("title"));
                    list.add(model);
                }
            }
        }
        return list;
    }


    @Override
    protected int setItemLayoutId(int viewType) {
        return R.layout.item_rv_main_township_dongtai;
    }

    @Override
    protected void bindItemData(RecyclerViewHolder holder, int position, final MineMessage model) {
        String title = model.getContent();
        if (title != null && title.length()>0){
            holder.setText(R.id.title, title.length()>12?title.substring(0,11) + "...":title);
        }
        if (model.getTime() != null && model.getTime().length()>0){
            holder.setText(R.id.time, model.getTime());
        }
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build("/main/MainTownShipDongTaiInfoUI")
                        .withString("id",model.getNid())
                        .navigation();

            }
        });
        
    }
    
}
